<?php
namespace app\admin\controller;
use app\admin\logic\WorkRecordDataTotalLogic;

/**
 * 每日数据统计api控制器
 * Created by PhpStorm.
 * User: tyll
 * Date: 2019/11/22
 * Time: 10:28
 */
class WorkRecordDataTotal extends Base {

    public function __construct(){
        parent::__construct();
        $this->wrdt_logic = new WorkRecordDataTotalLogic();
    }

    /**
     * 获取每日数据统计
     * @return \think\response\Json
     */
    public function ajax_get_wrdt(){
        $params = input();
        $r = $this->check_admin_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        //获取每日数据统计
        $r = $this->wrdt_logic->get_wrdt($params);

        $data['list'] = $r['data']['list'];
        return json($this->wrdt_logic->get_response(1,'OK',$data));
    }

    /**
     * 导出每日数据统计
     * http://127.0.0.1/mandiao-mini-api/public/admin-work_record_data_total-export_wrdt?token=e916a6039bff505bacaf5def2bb8435f&department=全部&start_time=&end_time=
     * @return \think\response\Json
     */
    public function export_wrdt(){
        $params = input();
        $r = $this->check_admin_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        //获取日记账统计
        $r = $this->wrdt_logic->get_wrdt($params);
        $list = $r['data']['list'];

        $row = ['部门名称','缺失次数','今日到店客户数','定金（元）','会员回款（元）','入驻回款（元）','代办回款（元）','代账回款（元）','商标回款（元）','科技服务（元）','其他（元）','目标金额（元）','目前金额（元）','完成率'];

        $export_data = [];
        foreach($list as $k => $v){
            $export_data[$k]['department'] = $v['department'];
            $export_data[$k]['data_miss_num'] = $v['data_miss_num'];
            $export_data[$k]['today_shop_person_num'] = $v['today_shop_person_num'];
            $export_data[$k]['front_money'] = $v['front_money'];
            $export_data[$k]['vip_back_money'] = $v['vip_back_money'];
            $export_data[$k]['check_in_back_money'] = $v['check_in_back_money'];
            $export_data[$k]['agency_back_money'] = $v['agency_back_money'];
            $export_data[$k]['account_back_money'] = $v['account_back_money'];
            $export_data[$k]['trademark_back_money'] = $v['trademark_back_money'];
            $export_data[$k]['science_money'] = $v['science_money'];
            $export_data[$k]['other_money'] = $v['other_money'];
            $export_data[$k]['target_money'] = $v['target_money'];
            $export_data[$k]['now_money'] = $v['now_money'];
            $finish_rate = $v['target_money'] > 0 ? ($v['now_money'] / $v['target_money']) : 0;
            $finish_rate = deal_number_mul_100($finish_rate);
            $finish_rate = round($finish_rate,0);
            $export_data[$k]['finish_rate'] = $finish_rate . '%';
        }
        array_unshift($export_data,$row);

        $file_name = '每日数据统计' . date('YmdHi') . '.xlsx';
        $r = $this->wrdt_logic->gen_wrdt_excel($file_name,$export_data);

        //$this->redirect(\think\facade\Request::root(true) . '/' . $file_name);
        return json($this->wrdt_logic->get_response(1,'导出成功'));
    }
}